<?php

class LinkageAction extends PublicAction
{
    public function area()
    {
        $module = M('area');
        $id = intval($_REQUEST['id']);
        $level = intval($_REQUEST['level']);
        $provinceid = intval($_REQUEST['level1_id']);
        $cityid = intval($_REQUEST['level2_id']);
        $areaid = intval($_REQUEST['level3_id']);

        $province_str = '<option value="0">请选择省份...</option>';
        $city_str = '<option value="0">请选择城市...</option>';
        $area_str = '<option value="0">请选择区域...</option>';
        $str = '';
        $r = $module->where("parentid='" . $id . "'")->select();

        foreach ($r as $key => $pro) {
            $selected = ($pro['id'] == $provinceid) ? ' selected="selected" ' : '';
            $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
        }
        if ($level == 0) {

            $province_str .= $str;
        } elseif ($level == 1) {
            $city_str .= $str;
        } elseif ($level == 2) {
            $area_str .= $str;
        }
        $str = '';
        if ($provinceid) {

            $rr = $module->where("parentid=" . $provinceid)->select();
            foreach ($rr as $key => $pro) {
                $selected = ($pro['id'] == $cityid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $city_str .= $str;
        }

        $str = '';
        if ($cityid) {
            $rrr = $module->where("parentid=" . $cityid)->select();
            foreach ($rrr as $key => $pro) {
                $selected = ($pro['id'] == $areaid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $area_str .= $str;
        }

        $res = array();
        $res['data'] = $rs ? 1 : 0;
        $res['linkage_1'] = $province_str;
        $res['linkage_2'] = $city_str;
        $res['linkage_3'] = $area_str;

        echo json_encode($res);
    }
}